

var path = require('path')
var routerApi = require('../../utils/routeString.js')(__filename, __dirname);
var utils = require('../../utils/index.js')
var validate = utils.validate;
var SqlHelper = utils.SqlHelper;
var jwt = require('../../utils/jwt.js')
var dateHelper = require('../../utils/date.js')
// var Queue = require('../../utils/Queue.js')


var dj = async function (req, res) {
    var body = req.body || req.query;

    
    return res.send({ code: 0, msg: '报名已满，谢谢。' });

    if (!body.token) {
        return res.send({ code: 0, msg: '无效请求, token error' });
    }

    // 授权确认
    var tokenData = await jwt.decode(body.token);
    if (!tokenData || !tokenData.userid) {
        return res.send({ code: 0, msg: '授权错误.' });
    }

    // 姓名不正确
    if (!validate.isName(body.name)) {
        return res.send({ code: 0, msg: '请填写正确的姓名' });
    }

    // 楼栋两位数以下
    if (!body.building || body.building.length < 1 || body.building.length > 2) {
        return res.send({ code: 0, msg: '您的楼栋信息错误，请检查' });
    }

    // 房间
    if (!body.room || body.room.length < 1 || body.room.length > 4) {
        return res.send({ code: 0, msg: '您的室号信息错误，请检查' });
    }

    // 人数
    if (body.number > 3) {
        return res.send({ code: 0, msg: '无法接受3人以上的报名，谢谢合作' });
    }

    // 宴请时间
    if (body.date != "2.9" && body.date != "2.11") {
        return res.send({ code: 0, msg: '请正确选择您的宴席时间' });
    }

    // 首先检查是否为业主

    var sqlHelper1 = new SqlHelper('ntvk_bm_20180201_yzinfo');
    var r1 = await sqlHelper1.asyncSelect({ building: body.building, room: body.room });

    //  非业主
    if (r1.length <= 0) {
        return res.send({ code: 0, msg: '抱歉，您不是业主，无法参加活动' });
    }

    if (r1[0].name1 != body.name && r1[0].name2 != body.name && r1[0].name3 != body.name && r1[0].name4 != body.name) {
        return res.send({ code: 0, msg: '抱歉，您不是业主，无法参加活动' });
    }

    // 已经报过名
    var sqlHelper2 = new SqlHelper('ntvk_bm_20180201');
    var r2 = await sqlHelper2.asyncSelect({ openid: tokenData.userid });
    if (r2.length > 0) {
        return res.send({ code: 0, msg: '抱歉，您已经报名参与了，无需重复报名' });
    }

    // 您已经有家人报名了
    var r4 = await sqlHelper2.asyncSelect({ building: body.building, room: body.room });
    if (r4.length > 0) {
        return res.send({ code: 0, msg: '抱歉，您已经有家人报名了，无需重复报名' });
    }

    // 报名人数过多
    const n1 = 280;
    const n2 = 550;

    var r3 = await sqlHelper2.asyncSelect({ date: body.date });
    var allnum = 0;

    for (let i = 0; i < r3.length; i++) {
        allnum += parseInt(r3[i].number);
    }

    // 报名已满
    if ((body.date == "2.9" && allnum >= n1) || (body.date == "2.11" && allnum >= n2)) {
        return res.send({ code: 0, msg: '抱歉，报名人数已满' });
    }



    // 所有条件都满足，可以报名了
    var data = {
        openid: tokenData.userid,
        name: body.name, // 姓名
        // phone: body.phone, // 电话号码
        building: body.building, // 楼栋 
        room: body.room, // 房间号
        number: body.number, // 人数
        date: body.date  // 日期
    }
    var result = await sqlHelper2.asyncInsert(data);

    return res.send({ code: 1, msg: '报名成功' });
}

var getuserdata = async function (req, res) {
    var body = req.body || req.query;

    if (!body.token) {
        return res.send({ code: 0, msg: '无效请求, token error' });
    }

    // 授权确认
    var tokenData = await jwt.decode(body.token);
    if (!tokenData || !tokenData.userid) {
        return res.send({ code: 0, msg: '服务器忙...1002' });
    }

    var sqlHelper = new SqlHelper('ntvk_bm_20180201');
    var userdata = await sqlHelper.asyncSelect({ openid: tokenData.userid });
    if (userdata.length > 0) {
        return res.send({ code: 1, userdata: userdata[0] });
    } else {
        return res.send({ code: 0, msg: "没有用户信息" });
    }
}


var login = async function (req, res, next) {
    var body = req.body || req.query;

    if (!body.account || !body.password) {
        return res.send({ code: 0, msg: '请输入您的账号和密码' });
    }

    if (body.account === "admin" && body.password === "matrixpassword2018") {
        var jwtData = await jwt.encode2(body.account);
        return res.send({ code: 1, token: jwtData.token });
    } else {
        return res.send({ code: 0, msg: '账号密码错误' });
    }

}

// return {list, count, comments}
var getTableData = async function (req, res) {
    var body = req.body || req.query;

    if (!body.token) {
        return res.send({ code: 0, msg: '账号密码错误' });
    }

    try {
        var userid = await jwt.decode(body.token);
        var sqlHelper = new SqlHelper('ntvk_bm_20180201');
        var result = await sqlHelper.asyncSelect(null, body.offset, body.limit);
        var result2 = await sqlHelper.asyncCount() //results[0].num
        var comments = await sqlHelper.asyncComments();
        return res.send({ code: 1, result: { list: result, count: result2[0].num, comments: comments } });
    } catch (error) {
        return res.send({ code: 0, msg: error.message });
    }

}


var analysisFn = function (req, res, next) {
    var body = req.body || req.query;

    var cmd = body.cmd;

    // 命令解析
    if (!cmd) {
        return res.send({ code: 0, msg: '无效请求, cmd参数出错' });
    }

    switch (cmd) {
        case 'dj':
            dj(req, res);  // 登记
            break;
        case 'getuserdata':
            getuserdata(req, res);  // 获取用户数据
            break;
        case "login":
            login(req, res, next);
            break;
        case "getTableData":
            getTableData(req, res);
            break;
        default:
            return res.send({ code: 0, msg: '无效请求, cmd参数出错' });
    }
}

server.post({ path: routerApi, name: routerApi }, analysisFn)